//Author: A0083934X

package Main;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.awt.Desktop;

public class Main {
	public static void main(String[] args) throws IOException {
		ManageItem obj = new ManageItem();
		ManageCategory e_cat=new ManageCategory();
		Undo undo = new Undo();
		Redo redo = new Redo();
		Scanner scan = new Scanner(System.in);
		int mainchoice = 10;
		double budget = 0;
		int loop; // loop for manage item
		int mainloop = 1; // loop for the main page

		final String datapath = System.getProperty("user.dir") + "\\src\\Database\\";
		final String excelpath = datapath + "item.xls";
		
		while (mainloop == 1) {
			System.out.println("Welcome to Manager v0.2");
			System.out.println("Please select your choice");
			System.out.println("1. Manage Item");
			System.out.println("2. Manage Expense Categories");
			System.out.println("3. Manage Payment Categories");
			System.out.println("4. Search");
			System.out.println("5. Display expense summary");
			System.out.println("6. View item list");
			System.out.println("7. View by Excel");
			System.out.println("8. Exit program ");
			
			
			if(mainchoice == 0){
				System.out.println("Please enter the input again: ");
			}
			try{
				mainchoice = scan.nextInt();
			} catch (InputMismatchException e){
				
				scan.next();
				mainchoice = 0;
			}
			if (mainchoice == 8) {	
				
				obj.saveData();
				
				obj.saveBudgetData();
				//exit
				System.out.println("Thank you for using Manager!");
				mainloop = 0;
			} 
			
			else if (mainchoice == 7){
				int excelloop = 1;
				boolean isNameWanted = false;
				boolean isPriceWanted = false;
				boolean isNecessityValueWanted = false;
				boolean isDayWanted = false;
				boolean isMonthWanted =false;
				boolean isYearWanted = false;
				boolean isPlaceWanted = false;
				boolean isCategoryWanted = false;
				boolean isPaymentWanted = false;
				
				while(excelloop == 1){
					System.out.println("What information you want to export? ");
					System.out.println("1. Name");
					System.out.println("2. Price");
					System.out.println("3. Necessity value");
					System.out.println("4. Day");
					System.out.println("5. Month");
					System.out.println("6. Year");
					System.out.println("7. Venue");
					System.out.println("8. Category");
					System.out.println("9. Payment Type");
					System.out.println("10. Export to Excel");
					int excelChoose = scan.nextInt();
					
					if (excelChoose == 1){
						isNameWanted = true;
					}
					else if(excelChoose == 2){
						isPriceWanted = true;
					}
					else if(excelChoose == 3){
						isNecessityValueWanted = true;
					}
					else if(excelChoose == 4){
						isDayWanted = true;
					}
					else if(excelChoose == 5){
						isMonthWanted = true;
					}
					else if(excelChoose == 6){
						isYearWanted = true;
					}
					else if(excelChoose == 7){
						isPlaceWanted = true;
					}
					else if(excelChoose == 8){
						isCategoryWanted = true;
					}
					else if(excelChoose == 9){
						isPaymentWanted = true;
					}
					
					else if(excelChoose == 10){
						excelloop = 0;

						obj.saveToExcel(isNameWanted, isPriceWanted, isNecessityValueWanted, isDayWanted, 
								isMonthWanted, isYearWanted, isPlaceWanted, isCategoryWanted, isPaymentWanted);
						Desktop.getDesktop().open(new File(excelpath));
						
					}
				}
															
			}
			
			else if (mainchoice == 1) {	//manage item
				String name;
				double price = 0;
				int nec = 0;
				int day = 0;
				int month = 0;
				int year = 0;
				String venue;
				String category;
				String payment;

				loop = 1;

				while (loop == 1) { // keep displaying the message
					
					int itemchoice = 1;
					
					System.out.println("What would you like to do?");
					System.out.println("1. Set Budget");
					System.out.println("2. Create new item");
					System.out.println("3. Delete existing item");
					System.out.println("4. Update existing item");
					System.out.println("5. Undo previous action");
					System.out.println("6. Redo");
					System.out.println("7. Back to main menu");
					
					try{
						itemchoice = scan.nextInt();
						if(itemchoice < 1 || itemchoice > 7){
							System.out.println("	***Invalid option!!***");
						}
					} catch(InputMismatchException e){
						scan.next();
						itemchoice = 0;
					}
					int noOfItem = 0;

					if (itemchoice == 1) {			//set budget
						int tempMonth = 0;
						int tempYear = 0;
						int check = 0;
						System.out.printf("Please enter the budget, the month, and the year [budget] [month] [year(2000-2014)]: ");
						
						try{
							budget = scan.nextDouble();
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println();
							System.out.println("	***Mismatch Budget input!***");
						}
						try{
							tempMonth = scan.nextInt();
							if(tempMonth < 1 || tempMonth > 12){
								check = 1;
								System.out.println();
								System.out.println("	***Month input is out of range (1 - 12)!!***");
								System.out.println();
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatch Month input!***");
						}
						try{
							tempYear = scan.nextInt();
							if(tempYear < 2000 || tempYear > 2014){
								check = 1;
								System.out.println();
								System.out.println("	***Year input is out of range (2000 - 2014)***");
								System.out.println();
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatch Year input!!***");
						}
						if(check == 0){
							System.out.println();
							obj.setBudget(budget, tempMonth, tempYear);
							System.out.println("The budget info has been stored successfully!");
							System.out.println();
						}
						else{
							System.out.println();
							System.out.println("The budget info cannot be stored!");
						}
					}

					if (itemchoice == 2) {			//create items
						noOfItem = 0;
						int check = 0;
						System.out.printf("How many items: ");
						try{
							noOfItem = scan.nextInt();
							if(noOfItem < 0){
								System.out.println("	***Invalid option!!***");
							}
						} catch(InputMismatchException e){
							scan.next();
							noOfItem = 0;
							System.out.println("	***Mismatch input!!***");
						}
						System.out.println();
						while (noOfItem > 0) {
							System.out
									.println("Please enter the following info:");
							System.out
									.println("(ITEM NAME(wihout space)) (Price) (Necessity Value [1 - 5]) [(DAY) (MONTH) (YEAR(2000 - 2014)) in integer] (Venue) (Category) (Payment Type)");
							
							name = scan.next();
							try{
								price = scan.nextDouble();
							} catch(InputMismatchException e){
								scan.next();
								check = 1;
								System.out.println("	***Mismatch price input!!***");
							}
							try{
								nec = scan.nextInt();
							} catch(InputMismatchException e){
								scan.next();
								check = 1;
								System.out.println("	***Mismatch necessity value input!!***");
							}
							try{
								day = scan.nextInt();
								if(day < 1 || day > 31){
									check = 1;
									System.out.println("	***Day input out of range (1 - 31)!***");
								}
							} catch(InputMismatchException e){
								scan.next();
								check = 1;
								System.out.println("	***Mismatch day input!!***");
							}
							try{
								month = scan.nextInt();
								if(month < 1 || month > 12){
									check = 1;
									System.out.println("	***Month input out of range (1 - 12)!!***");
								}
							} catch(InputMismatchException e){
								scan.next();
								check = 1;
								System.out.println("	***Mismatch Month input!!***");
							}
							try{
								year = scan.nextInt();
								if(year < 2000 || year > 2014){
									check = 1;
									System.out.println("	***Year input out of range (2000 - 2014)!!***");
								}
							} catch(InputMismatchException e){
								scan.next();
								check = 1;
								System.out.println("	***Mismatch year input!!***");
							}
							venue = scan.next();
							
							category = scan.next();
							
							payment = scan.next();
							
							if(check == 0){
								obj.addToItemVector(name, price, nec, day, month, year,
									venue, category,payment);
								noOfItem--;
								Item i=new Item(name, price, nec, day, month, year,
									venue, category,payment);
								undo.addUndo(i, "create");
								System.out.println("Item has been successfully added!");
								System.out.println();
							}
							else{
								System.out.println("The item information cannot be stored!!");
								noOfItem = 0;
							}
						}
						Vector <Item> forprinting = new Vector<Item>();
						forprinting = obj.displayItemVector();
						System.out.println("Item(s) on the list: ");
						System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
						for(int i =0; i < forprinting.size(); i++){
							System.out.printf("| NAME: %s \t PRICE: %.2f \t NECESSITY VALUE: %d \t DAY: %d  MONTH: %d  YEAR: %d \t PLACE: %s " +
									"\t CATEGORY: %s \t PAYMENT: %s \t \n" ,forprinting.get(i).getName(), forprinting.get(i).getPrice(), forprinting.get(i).getNecessityValue(), forprinting.get(i).getDate().getDay(), forprinting.get(i).getDate().getMonth(), forprinting.get(i).getDate().getYear(), forprinting.get(i).getPlace(), forprinting.get(i).getCategory(), forprinting.get(i).getPay());
							System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
						}
					} else if (itemchoice == 3) {		//delete item
						int check = 0;
						if(obj.getNoOfItem() == 0){
							System.out.println("There is no item in the list!!");
							System.out.println();
							continue;
						}
						
						System.out
								.println("Please enter the item information that you want to delete:");
						System.out
								.println("(ITEM NAME) (Price) (Necessity Value [1 - 5]) [(DAY) (MONTH) (YEAR(2000 - 2014)) in integer] (Venue) (Category) (Payment Type)");
						name = scan.next();
						try{
							price = scan.nextDouble();
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatch price input!!***");
						}
						try{
							nec = scan.nextInt();
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatch necessity value input!!***");
						}
						try{
							day = scan.nextInt();
							if(day < 1 || day > 31){
								check = 1;
								System.out.println("	***Day input out of range (1 - 31)!***");
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatch day input!!***");
						}
						try{
							month = scan.nextInt();
							if(month < 1 || month > 12){
								check = 1;
								System.out.println("	***Month input out of range (1 - 12)!!***");
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatch Month input!!***");
						}
						try{
							year = scan.nextInt();
							if(year < 2000 || year > 2014){
								check = 1;
								System.out.println("	***Year input out of range (2000 - 2014)!!***");
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatch year input!!***");
						}
						venue = scan.next();
						category = scan.next();
						payment = scan.next();
						if(check == 0){
							Item temp = new Item(name, price, nec, day, month,
								year, venue, category, payment);
							int itemExist;
							itemExist = obj.deleteFromItemVector(temp);
						
							if(itemExist == -1){
								System.out.println("The item cannot be found!");
								System.out.println();
							}
							else{
								undo.addUndo(temp, "delete", itemExist);
								System.out.println("The item has been successfully deleted!");
								System.out.println();
								
								Vector <Item> forprinting = new Vector<Item>();
								forprinting = obj.displayItemVector();
								System.out.println("Item(s) on the list: ");
								System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
								for(int i =0; i < forprinting.size(); i++){
									System.out.printf("| NAME: %s \t PRICE: %.2f \t NECESSITY VALUE: %d \t DAY: %d  MONTH: %d  YEAR: %d \t PLACE: %s " +
											"\t CATEGORY: %s \t PAYMENT: %s \t \n" ,forprinting.get(i).getName(), forprinting.get(i).getPrice(), forprinting.get(i).getNecessityValue(), forprinting.get(i).getDate().getDay(), forprinting.get(i).getDate().getMonth(), forprinting.get(i).getDate().getYear(), forprinting.get(i).getPlace(), forprinting.get(i).getCategory(), forprinting.get(i).getPay());
									System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
								}
							}
						}
						else{
							System.out.println("Action cancelled due to mismatch of expected info!!");
							System.out.println();
						}
					} else if (itemchoice == 4) {		//update item
						int check = 0;
						
						if(obj.getNoOfItem() == 0){
							System.out.println("There is no item in the list!!");
							System.out.println();
							continue;
						}
						
						System.out
								.println("Please enter the item information that you want to update: ");
						System.out
								.println("(ITEM NAME) (Price) (Necessity Value [1 - 5]) [(DAY) (MONTH) (YEAR(2000-2014)) in integer] (Venue) (Category) (Payment Type)");
						name = scan.next();
						try{
							price = scan.nextDouble();
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatch price input!!***");
						}
						try{
							nec = scan.nextInt();
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatch necessity value input!!***");
						}
						try{
							day = scan.nextInt();
							if(day < 1 || day > 31){
								check = 1;
								System.out.println("	***Day input out of range (1 - 31)!***");
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatch day input!!***");
						}
						try{
							month = scan.nextInt();
							if(month < 1 || month > 12){
								check = 1;
								System.out.println("	***Month input out of range (1 - 12)!!***");
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatch Month input!!***");
						}
						try{
							year = scan.nextInt();
							if(year < 2000 || year > 2014){
								check = 1;
								System.out.println("	***Year input out of range (2000 - 2014)!!***");
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatch year input!!***");
						}
						venue = scan.next();
						category = scan.next();
						payment = scan.next();
						
						if(check == 0){
							Item temp1 = new Item(name, price, nec, day, month,
								year, venue, category, payment);
							Search getData;
							getData = obj.managerSearch(temp1);
							Vector<Item> tocheck = getData.getMatchingItem();
							if(tocheck.size() == 0){
								System.out.println("No items found!!");
								System.out.println();
								continue;
							}
							System.out
								.println("Please enter the new information of the item:");
							System.out
								.println("(ITEM NAME) (Price) (Necessity Value [1 - 5]) [(DAY) (MONTH) (YEAR(2000-2014)) in integer] (Venue) (Category) (Payment Type)");
							name = scan.next();
							try{
								price = scan.nextDouble();
							} catch(InputMismatchException e){
								scan.next();
								check = 1;
								System.out.println("	***Mismatch price input!!***");
							}
							try{
								nec = scan.nextInt();
							} catch(InputMismatchException e){
								scan.next();
								check = 1;
								System.out.println("	***Mismatch necessity value input!!***");
							}
							try{
								day = scan.nextInt();
								if(day < 1 || day > 31){
									check = 1;
									System.out.println("	***Day input out of range (1 - 31)!***");
								}
							} catch(InputMismatchException e){
								scan.next();
								check = 1;
								System.out.println("	***Mismatch day input!!***");
							}
							try{
								month = scan.nextInt();
								if(month < 1 || month > 12){
									check = 1;
									System.out.println("	***Month input out of range (1 - 12)!!***");
								}
							} catch(InputMismatchException e){
								scan.next();
								check = 1;
								System.out.println("	***Mismatch Month input!!***");
							}
							try{
								year = scan.nextInt();
								if(year < 2000 || year > 2014){
									check = 1;
									System.out.println("	***Year input out of range (2000 - 2014)!!***");
								}
							} catch(InputMismatchException e){
								scan.next();
								check = 1;
								System.out.println("	***Mismatch year input!!***");
							}
							venue = scan.next();
							category = scan.next();
							payment = scan.next();
							if(check == 0){
								Item temp2 = new Item(name, price, nec, day, month,
									year, venue, category, payment);
								int itemExist;
								itemExist = obj.updateItemVector(temp1, temp2);
								if(itemExist == -1){
									System.out.println("The item cannot be found!");
									System.out.println();
								}
								else if(itemExist == 1){
									undo.addUndo(temp1, temp2, "update");
									System.out.println("The item has been successfully updated!");
									System.out.println();
									
									Vector <Item> forprinting = new Vector<Item>();
									forprinting = obj.displayItemVector();
									System.out.println("Item(s) on the list: ");
									System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
									for(int i =0; i < forprinting.size(); i++){				
										System.out.printf("| NAME: %s \t PRICE: %.2f \t NECESSITY VALUE: %d \t DAY: %d  MONTH: %d  YEAR: %d \t PLACE: %s " +
												"\t CATEGORY: %s \t PAYMENT: %s \t \n" ,forprinting.get(i).getName(), forprinting.get(i).getPrice(), forprinting.get(i).getNecessityValue(), forprinting.get(i).getDate().getDay(), forprinting.get(i).getDate().getMonth(), forprinting.get(i).getDate().getYear(), forprinting.get(i).getPlace(), forprinting.get(i).getCategory(), forprinting.get(i).getPay());
										System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
									}
								}
							}
							else{
								System.out.println("Action cancelled due to mismatch of expected info!!");
								System.out.println();
							}
						}
						else{
							System.out.println("Action cancelled due to mismatch of expected info!!");
							System.out.println();
						}
					} else if (itemchoice==5){			//undo CRUD action
						
						if(undo.check() == true){
							System.out.println("There is nothing to undo!");
							System.out.println();
						}
						else{
							DataForUndo data = undo.UndoAction();
							if((data.getType()).equals("create")){			//undo the create by deleting the item
						
								int temp = obj.deleteFromItemVector(data.getItem());
								redo.addRedo(data.getItem(), "create", temp);
								System.out.println("Undo is successful!");
								System.out.println();
							}
							else if((data.getType()).equals("delete")){		//undo the delete by creating the item
								redo.addRedo(data.getItem(), "delete");
								int tempind = data.getIndex();
								obj.addToItemVectorUndo(data.getItem().getName(), data.getItem().getPrice(),data.getItem().getNecessityValue(),
									data.getItem().getDate().getDay(), data.getItem().getDate().getMonth(), 
									data.getItem().getDate().getYear(), data.getItem().getPlace(), data.getItem().getCategory(), data.getItem().getPay(), tempind);
								System.out.println("Undo is successful!");
								System.out.println();
							}
							else if ((data.getType()).equals("update")){
								redo.addRedo(data.getItemUold(), data.getItemUnew(), "update");
								obj.updateItemVector(data.getItemUnew(), data.getItemUold());
								System.out.println("Undo is successful!");
								System.out.println();
							}
							else{
								System.out.println("There is nothing to undo!");
								System.out.println();
							}
							
						}
					}
					else if(itemchoice == 6){			//redo
						if(redo.check() == true){
							System.out.println("There is nothing to redo!");
							System.out.println();
						}
						else{
							DataForRedo data=redo.RedoAction();
							if((data.getType()).equals("create")){			//redo the create  
								undo.addUndo(data.getItem(), "create");
								obj.addToItemVectorUndo(data.getItem().getName(), data.getItem().getPrice(),data.getItem().getNecessityValue(),
									data.getItem().getDate().getDay(), data.getItem().getDate().getMonth(), 
									data.getItem().getDate().getYear(), data.getItem().getPlace(), data.getItem().getCategory(), data.getItem().getPay(), data.getIndex());

								System.out.println("Redo Successful!");
							}
							else if((data.getType()).equals("delete")){		//redo the delete 
								int tempind = obj.deleteFromItemVector(data.getItem());
								undo.addUndo(data.getItem(), "delete", tempind);
								System.out.println("Redo Successful!");
							}
							else if ((data.getType()).equals("update")){
								undo.addUndo(data.getItemUold(), data.getItemUnew(), "update");
								obj.updateItemVector(data.getItemUold(), data.getItemUnew());
								System.out.println("Redo Successful!");
							}
							else{
								System.out.println("There is nothing to redo!");
								System.out.println();
							}
						}
					}
					else if (itemchoice == 7) {		//back to main menu
						loop = 0;
					}
					else{
						
						System.out.println();
					}
				}
			} //end of main choice 1
			
			else if(mainchoice == 2){			//manage expense categories
				loop = 1;

				while (loop == 1) { // keep displaying the message
					
					int itemchoice = 1;
					
					System.out.println("1. Create new expense category");
					System.out.println("2. Delete existing expense category");
					System.out.println("3. Update existing expense category");
					System.out.println("4. Undo");
					System.out.println("5. Redo");
					System.out.println("6. Display current category/categories");
					System.out.println("7. Back to main menu");
					
					try{
						itemchoice = scan.nextInt();
						if(itemchoice < 1 || itemchoice > 7){
							System.out.println("	***Invalid option!!***");
						}
					} catch(InputMismatchException e){
						scan.next();
						itemchoice = 0;
					}
					switch(itemchoice){
					case(1):{
						System.out.println("Please enter the name of the expense category you wish to create");
						String name=scan.next();
						int check;
						check = e_cat.create(name, "expense");
						if(check == 0){
							System.out.println("The expense category has been successfully created!");
							System.out.println();
							//add code for undo
							undo.addUndo(name, "createex");
						}
						else{
							System.out.println("Expense Category already exists!\n");
						}
						
					}break;
					case(2): {
						System.out.println("Please enter the name of the expense category you wish to delete");
						String name=scan.next();
						int check;
						check = e_cat.delete(name, obj, "expense");
						System.out.println();
						//add code for undo
						if(check != -1){
							undo.addUndo(name, "deleteex");
							System.out.println("The expense category has been successfully deleted!");
						}
						else{
							System.out.println("The expense category cannot be found!");
						}
						
					}break;
					case(3): {
						System.out.println("Please enter the name of the expense category you wish to update");
						String name_old=scan.next();
						System.out.println("Please enter the new name of the expense categoy");
						String name_new=scan.next();
						int check;
						check = e_cat.update(name_old, name_new, obj, "expense");
						System.out.println();
						//add code for undo
						if(check != -1){
							undo.addUndo(name_old, name_new, "updateex");
							System.out.println("The expense category has been successfully updated!");
						}
						else{
							System.out.println("The expense category cannot be found!");
						}
						
					}break;
					case(4): {
						if(undo.check() == true){
							System.out.println("There is nothing to undo!");
							System.out.println();
						}
						else{
							DataForUndo data = undo.UndoAction();
							if(data.getType().equals("createex")){
								String name = data.getName();
								int check;
								check = e_cat.delete(name, obj, "expense");
								redo.addRedo(name, "createex");
								
								System.out.println();
								System.out.println("Undo successful!");
								System.out.println();
							}
							else if(data.getType().equals("deleteex")){
								String name = data.getName();
								e_cat.create(name, "expense");
								e_cat.change(obj, name, "expense");
								redo.addRedo(name, "deleteex");
								
								System.out.println();
								System.out.println("Undo successful!");
								System.out.println();
							}
							else if(data.getType().equals("updateex")){
								String name_old = data.getNameNew();
								String name_new = data.getNameOld();
								int check = e_cat.update(name_old, name_new, obj, "expense");
								redo.addRedo(name_old, name_new, "updateex");
								
								System.out.println();
								System.out.println("Undo successful!");
								System.out.println();
							}
							else{
								System.out.println("There is nothing to undo!");
								System.out.println();
							}
						}
					}break;
					
					case(5): {
						if(redo.check() == true){
							System.out.println("There is nothing to redo!");
							System.out.println();
						}
						else{
							DataForRedo data = redo.RedoAction();
							if(data.getType().equals("createex")){
								String name = data.getName();
								e_cat.create(name, "expense");
								e_cat.change(obj, name, "expense");
								undo.addUndo(name, "createex");
								
								System.out.println();
								System.out.println("Redo successful!");
								System.out.println();
							}
							else if(data.getType().equals("deleteex")){
								String name = data.getName();
								int check;
								check = e_cat.delete(name, obj, "expense");
								undo.addUndo(name, "deleteex");
								
								System.out.println();
								System.out.println("Redo successful!");
								System.out.println();
							}
							else if(data.getType().equals("updateex")){
								String name_old = data.getNameNew();
								String name_new = data.getNameOld();
								int check = e_cat.update(name_old, name_new, obj, "expense");
								undo.addUndo(name_old, name_new, "updateex");
								
								System.out.println();
								System.out.println("Redo successful!");
								System.out.println();
							}
							else{
								System.out.println("There is nothing to redo!");
								System.out.println();
							}
						}
					}break;
					case(6):{
						Vector<Category> temp=e_cat.print(obj, "expense");
						for(int i=0;i<temp.size();i++){
							System.out.println("Category name: "+temp.get(i).getName());
							System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
							for(int j =0; j < temp.get(i).getCatItem().size(); j++){
								System.out.printf("| NAME: %s \t PRICE: %.2f \t NECESSITY VALUE: %d \t DAY: %d  MONTH: %d  YEAR: %d \t PLACE: %s " +
										"\t CATEGORY: %s \t PAYMENT: %s \t \n" ,temp.get(i).getCatItem().get(j).getName(), temp.get(i).getCatItem().get(j).getPrice(), temp.get(i).getCatItem().get(j).getNecessityValue(), temp.get(i).getCatItem().get(j).getDate().getDay(), temp.get(i).getCatItem().get(j).getDate().getMonth(), temp.get(i).getCatItem().get(j).getDate().getYear(), temp.get(i).getCatItem().get(j).getPlace(), temp.get(i).getCatItem().get(j).getCategory(), temp.get(i).getCatItem().get(j).getPay());
								System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
							}
							System.out.println();
						}
					} break;
					case(7): {
						loop=0;
					}break;
					default: 
						System.out.println();
					}
				}
			} //end of main choice 2
			
			else if(mainchoice == 3){			//manage payment categories
				loop = 1;

				while (loop == 1) { // keep displaying the message
					
					int itemchoice = 1;
					
					System.out.println("1. Create new payment category");
					System.out.println("2. Delete existing payment category");
					System.out.println("3. Update existing payment category");
					System.out.println("4. Undo previous action");
					System.out.println("5. Redo");
					System.out.println("6. Display current category/categories");
					System.out.println("7. Back to main menu");
					
					try{
						itemchoice = scan.nextInt();
						if(itemchoice < 1 || itemchoice > 7){
							System.out.println("	***Invalid option!!***");
						}
					} catch(InputMismatchException e){
						scan.next();
						itemchoice = 0;
					}
					switch(itemchoice){
					case(1):{
						System.out.println("Please enter the name of the payment category you wish to create");
						String name=scan.next();
						int check;
						check = e_cat.create(name, "payment");
						if(check == 0){
							System.out.println("The payment category has been successfully created!");
							System.out.println();
							undo.addUndo(name, "createpay");
						}
						else{
							System.out.println("Payment Category already exists!\n");
						}
						
					}break;
					case(2): {
						System.out.println("Please enter the name of the payment category you wish to delete");
						String name=scan.next();
						int check;
						check = e_cat.delete(name, obj, "payment");
						System.out.println();
						if(check != -1){
							undo.addUndo(name, "deletepay");
							System.out.println("The payment category has been successfully deleted!");
						}
						else{
							System.out.println("The payment category cannot be found!");
						}
					}break;
					case(3): {
						System.out.println("Please enter the name of the payment category you wish to update");
						String name_old=scan.next();
						System.out.println("Please enter the new name of the payment categoy");
						String name_new=scan.next();
						int check;
						check = e_cat.update(name_old, name_new, obj, "payment");
						System.out.println();
						if(check != -1){
							undo.addUndo(name_old, name_new, "updatepay");
							System.out.println("The payment category has been successfully updated!");
						}
						else{
							System.out.println("The payment category cannot be found!");
						}
					}break;
					case(4): {
						if(undo.check() == true){
							System.out.println("There is nothing to undo!");
							System.out.println();
						}
						else{
							DataForUndo data = undo.UndoAction();
							if(data.getType().equals("createpay")){
								String name = data.getName();
								int check;
								check = e_cat.delete(name, obj, "payment");
								redo.addRedo(name, "createpay");
								
								System.out.println();
								System.out.println("Undo successful!");
								System.out.println();
							}
							else if(data.getType().equals("deletepay")){
								String name = data.getName();
								e_cat.create(name, "payment");
								e_cat.change(obj, name, "payment");
								redo.addRedo(name, "deletepay");
								
								System.out.println();
								System.out.println("Undo successful!");
								System.out.println();
							}
							else if(data.getType().equals("updatepay")){
								String name_old = data.getNameNew();
								String name_new = data.getNameOld();
								int check = e_cat.update(name_old, name_new, obj, "payment");
								redo.addRedo(name_old, name_new, "updatepay");
								
								System.out.println();
								System.out.println("Undo successful!");
								System.out.println();
							}
							else{
								System.out.println("There is nothing to undo!");
								System.out.println();
							}
						}
						
					}break;
					
					case(5): {
						if(redo.check() == true){
							System.out.println("There is nothing to redo!");
							System.out.println();
						}
						else{
							DataForRedo data = redo.RedoAction();
							if(data.getType().equals("createpay")){
								String name = data.getName();
								e_cat.create(name, "payment");
								e_cat.change(obj, name, "payment");
								undo.addUndo(name, "createpay");
								
								System.out.println();
								System.out.println("Redo successful!");
								System.out.println();
							}
							else if(data.getType().equals("deletepay")){
								String name = data.getName();
								int check;
								check = e_cat.delete(name, obj, "payment");
								undo.addUndo(name, "deletepay");
								
								System.out.println();
								System.out.println("Redo successful!");
								System.out.println();
							}
							else if(data.getType().equals("updatepay")){
								String name_old = data.getNameNew();
								String name_new = data.getNameOld();
								int check = e_cat.update(name_old, name_new, obj, "payment");
								undo.addUndo(name_old, name_new, "updatepay");
								
								System.out.println();
								System.out.println("Redo successful!");
								System.out.println();
							}
							else{
								System.out.println("There is nothing to redo!");
								System.out.println();
							}
						}
					}break;
					case(6):{
						Vector<Category> temp=e_cat.print(obj, "payment");
						for(int i=0;i<temp.size();i++){
							System.out.println("Category name: "+temp.get(i).getName());
							System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
							for(int j =0; j < temp.get(i).getCatItem().size(); j++){
								System.out.printf("| NAME: %s \t PRICE: %.2f \t NECESSITY VALUE: %d \t DAY: %d  MONTH: %d  YEAR: %d \t PLACE: %s " +
										"\t CATEGORY: %s \t PAYMENT: %s \t \n" ,temp.get(i).getCatItem().get(j).getName(), temp.get(i).getCatItem().get(j).getPrice(), temp.get(i).getCatItem().get(j).getNecessityValue(), temp.get(i).getCatItem().get(j).getDate().getDay(), temp.get(i).getCatItem().get(j).getDate().getMonth(), temp.get(i).getCatItem().get(j).getDate().getYear(), temp.get(i).getCatItem().get(j).getPlace(), temp.get(i).getCatItem().get(j).getCategory(), temp.get(i).getCatItem().get(j).getPay());
								System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
							}
							System.out.println();
						}
					} break;
					case(7): {
						loop=0;
					}break;
					default: 
						System.out.println();
					}
				}
			} //end of main choice 3
			
			else if(mainchoice == 4){			//search feature
				int searchchoice = 0;
				Vector <Item> forprinting = new Vector <Item>();
				System.out.println("What kind of search do you want to use?");
				System.out.println("1. Simple search");
				System.out.println("2. Advance search");
				try{
					searchchoice = scan.nextInt();
					if(searchchoice < 1 || searchchoice > 2){
						System.out.println("	***Invalid option!!***");
					}
				} catch (InputMismatchException e){
					scan.next();
					System.out.println("	***Mismatch input!!***");
				}
				if(searchchoice == 1){			//simple search
					System.out.printf("Enter the name of the item: ");
					String nametosearch = scan.next();
					System.out.println();
					Item userinput = new Item(nametosearch);
					Search getData;
					getData = obj.managerSearch(userinput);
					forprinting = getData.getMatchingItem();
					if(forprinting.size() == 0){
						System.out.println();
						System.out.println("No item matched!");
						System.out.println();
					}
					else{
						System.out.println();
						System.out.println("Total of " + forprinting.size() + " item(s) are found!");
						System.out.println();
					}
					System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
					for(int i =0; i < forprinting.size(); i++){
						System.out.printf("| NAME: %s \t PRICE: %.2f \t NECESSITY VALUE: %d \t DAY: %d  MONTH: %d  YEAR: %d \t PLACE: %s " +
								"\t CATEGORY: %s \t PAYMENT: %s \t \n" ,forprinting.get(i).getName(), forprinting.get(i).getPrice(), forprinting.get(i).getNecessityValue(), forprinting.get(i).getDate().getDay(), forprinting.get(i).getDate().getMonth(), forprinting.get(i).getDate().getYear(), forprinting.get(i).getPlace(), forprinting.get(i).getCategory(), forprinting.get(i).getPay());
						System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
					}
					System.out.println();
				}
				else if(searchchoice == 2){		//advance search
					int advanceloop = 1;
					String advancename = null;
					double advanceprice = 0;
					int advancenec = 0;
					int advanceday = 0;
					int advancemonth = 0;
					int advanceyear = 0;
					String advancevenue = null;
					String advancecategory = null;
					String advancepay = null;
					
					while(advanceloop == 1){
						System.out.println("Search by: ");
						System.out.println("1. Name");
						System.out.println("2. Price");
						System.out.println("3. Necessity value");
						System.out.println("4. Day");
						System.out.println("5. Month");
						System.out.println("6. Year");
						System.out.println("7. Venue");
						System.out.println("8. Category");
						System.out.println("9. Payment Type");
						System.out.println("10. Show results");
						int advancechoice = scan.nextInt();
						
						if(advancechoice == 1){
							System.out.printf("Please enter the name: ");
							advancename = scan.next();
						}
						else if(advancechoice == 2){
							System.out.printf("Please enter the price: ");
							try{
								advanceprice = scan.nextDouble();
							} catch(InputMismatchException e){
								scan.next();
								System.out.println("	***Mismatched input!!***");
							}
						}
						else if(advancechoice == 3){
							System.out.printf("Please enter the necessity value: ");
							try{
								advancenec = scan.nextInt();
							} catch(InputMismatchException e){
								scan.next();
								System.out.println("	***Mismatched input!!***");
							}
						}
						else if(advancechoice == 4){
							System.out.printf("Please enter the day in integer: ");
							try{
								advanceday = scan.nextInt();
							} catch(InputMismatchException e){
								scan.next();
								System.out.println("	***Mismatched input!!***");
							}
						}
						else if(advancechoice == 5){
							System.out.printf("Please enter the month in integer: ");
							try{
								advancemonth = scan.nextInt();
							} catch(InputMismatchException e){
								scan.next();
								System.out.println("	***Mismatched input!!***");
							}
						}
						else if(advancechoice == 6){
							System.out.printf("Please enter the year: ");
							try{
								advanceyear = scan.nextInt();
							} catch(InputMismatchException e){
								scan.next();
								System.out.println("	***Mismatched input!!***");
							}
						}
						else if(advancechoice == 7){
							System.out.printf("Please enter the place: ");
							advancevenue = scan.next();
						}
						else if(advancechoice == 8){
							System.out.printf("Please enter the category: ");
							advancecategory = scan.next();
						}
						else if(advancechoice == 9){
							System.out.printf("Please enter the payment type: ");
							advancepay = scan.next();
						}
						else if(advancechoice == 10){
							advanceloop = 0;
						}
					} //end of advanceloop 
					Item userinput = new Item (advancename, advanceprice, 
							advancenec, advanceday, advancemonth, advanceyear, advancevenue, advancecategory, advancepay);
					Search getData;
					getData = obj.managerSearch(userinput);
					forprinting = getData.getMatchingItem();
					if(forprinting.size() == 0){
						System.out.println();
						System.out.println("No item matched!");
						System.out.println();
					}
					else{
						System.out.println();
						System.out.println("Total of " + forprinting.size() + " item(s) are found!");
						System.out.println();
					
						System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
						for(int i =0; i < forprinting.size(); i++){
							System.out.printf("| NAME: %s \t PRICE: %.2f \t NECESSITY VALUE: %d \t DAY: %d  MONTH: %d  YEAR: %d \t PLACE: %s " +
									"\t CATEGORY: %s \t PAYMENT: %s \t \n" ,forprinting.get(i).getName(), forprinting.get(i).getPrice(), forprinting.get(i).getNecessityValue(), forprinting.get(i).getDate().getDay(), forprinting.get(i).getDate().getMonth(), forprinting.get(i).getDate().getYear(), forprinting.get(i).getPlace(), forprinting.get(i).getCategory(), forprinting.get(i).getPay());
							System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
						}// end for loop
					}
					System.out.println();
				} //end of advance search
				else{
					
					System.out.println();
				}
			} // end of main choice 4
			else if(mainchoice == 5){			//summary expense
				int summaryChoice = 0;
				
				System.out.println("Please select the choices of the summary: ");
				System.out.println("1. Budget Reports");
				System.out.println("2. Total Expense");
				try{
					summaryChoice = scan.nextInt();
					if(summaryChoice < 1 || summaryChoice > 2){
						System.out.println("	***Invalid option!!***");
					}
				} catch(InputMismatchException e){
					scan.next();
					System.out.println("	***Mismatched input!!***");
				}
				if(summaryChoice == 1){
					int check = 0;
					System.out.printf("Please enter the [MONTH] and [YEAR(2000 - 2014)]: ");
					int monthReport = 0;
					try{
						monthReport = scan.nextInt();
						if(monthReport < 1 || monthReport > 12){
							check = 1;
							System.out.println("	***Month info is out of range (1 - 12)!!!***");
						}
					} catch(InputMismatchException e){
						scan.next();
						check = 1;
						System.out.println("	***Mismatched month input!!***");
					}
					int yearReport = 0;
					try{
						yearReport = scan.nextInt();
						if(yearReport < 2000 || yearReport > 2014){
							check = 1;
							System.out.println("	***Year info is out of range (2000 - 2014)!!***");
						}
					} catch(InputMismatchException e){
						scan.next();
						check = 1;
						System.out.println("	***Mismatched year input!!***");
					}
					Expense exp;
					if(check == 0){
						exp = obj.getReportAndAlert(monthReport, yearReport);
						System.out.println("--------------------------------------------------------");
						System.out.println("Summary of the expense: ");
						System.out.printf("Budget: %.2f\n", exp.getBudget());
						System.out.printf("Spending: %.2f\n", exp.getSpending());
						double remaining = exp.getBudget() - exp.getSpending();
						System.out.printf("Remaining: %.2f\n", remaining);
						System.out.println("Alert: " + exp.getAlert());
						System.out.println("--------------------------------------------------------");
					}
					else{
						System.out.println("Action cancelled due to Mismatched info!!");
						System.out.println();
					}
				}
				else if(summaryChoice == 2){
					System.out.println("Please Select the choices: ");
					System.out.println("1. Monthly Expense");
					System.out.println("2. Daily Expense");
					int tempchoice  = 0;
					try{
						tempchoice = scan.nextInt();
						if(tempchoice < 1 || tempchoice > 2){
							System.out.println("	***Invalid option!!***");
						}
					} catch(InputMismatchException e){
						scan.next();
						System.out.println("	***Mismatched input!!***");
					}
					if(tempchoice == 1){
						int check = 0;
						System.out.println("Please enter the [MONTH] and the [YEAR(2000 - 2014)]: ");
						int tempmonth = 0;
						try{
							tempmonth = scan.nextInt();
							if(tempmonth < 1 || tempmonth > 12){
								check = 1;
								System.out.println("	***Month info is out of range (1 - 12)!!!***");
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatched month input!!***");
						}
						int tempyear = 0;
						try{
							tempyear = scan.nextInt();
							if(tempyear < 2000 || tempyear > 2014){
								check = 1;
								System.out.println("	***Year info is out of range (2000 - 2014)!!!***");
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatched year input!!***");
						}
						if(check == 0){
							Expense monthly = obj.getMonthlyExpense(tempmonth, tempyear);
							System.out.printf("Monthly Expense: %.2f\n",  monthly.getSpending());
							System.out.println();
						}
						else{
							System.out.println("Action cancelled due to mismatched expected input!!");
							System.out.println();
						}
					}
					else if(tempchoice == 2){
						int check = 0;
						System.out.println("Please enter the {DAY}, [MONTH] and the [YEAR (2000 - 2014)]: ");
						int tempday = 0;
						try{
							tempday = scan.nextInt();
							if(tempday < 1 || tempday > 31){
								check = 1;
								System.out.println("	***Day info is out of range (1 - 31)!!!***");
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatched day input!!***");
						}
						int tempmonth = 0;
						try{
							tempmonth = scan.nextInt();
							if(tempmonth < 1 || tempmonth > 12){
								check = 1;
								System.out.println("	***Month info is out of range (1 - 12)!!!***");
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatched month input!!***");
						}
						int tempyear = 0;
						try{
							tempyear = scan.nextInt();
							if(tempyear < 2000 || tempyear > 2014){
								check = 1;
								System.out.println("	***Year info is out of range (2000 - 2014)!!!***");
							}
						} catch(InputMismatchException e){
							scan.next();
							check = 1;
							System.out.println("	***Mismatched year input!!***");
						}
						if(check == 0){
							Expense daily = obj.getDailyExpense(tempday, tempmonth, tempyear);
							System.out.printf("Daily Expense: %.2f\n", daily.getSpending());
							System.out.println();
						}
						else{
							System.out.println("Action cancelled due to mismatched expected input!!");
							System.out.println();
						}
					}
				}
				
			} //end of main choice 3
			else if(mainchoice == 6){		//display item list
				Vector <Item> forprinting = new Vector<Item>();
				forprinting = obj.displayItemVector();
				System.out.println("Item(s) on the list: ");
				System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
				for(int i =0; i < forprinting.size(); i++){
					System.out.printf("| NAME: %s \t PRICE: %.2f \t NECESSITY VALUE: %d \t DAY: %d  MONTH: %d  YEAR: %d \t PLACE: %s " +
							"\t CATEGORY: %s \t PAYMENT: %s \t \n" ,forprinting.get(i).getName(), forprinting.get(i).getPrice(), forprinting.get(i).getNecessityValue(), forprinting.get(i).getDate().getDay(), forprinting.get(i).getDate().getMonth(), forprinting.get(i).getDate().getYear(), forprinting.get(i).getPlace(), forprinting.get(i).getCategory(), forprinting.get(i).getPay());
					System.out.printf(" ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
				}// end for loop
				System.out.println();
			} //end of main choice 4
			else{
				System.out.println("	***Invalid input!***");
				System.out.println();
			}
		}// end of main page
	} //end of static method
} //end of class
